package com.dfbz.config;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.logging.log4j2.Log4j2Impl;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tk.mybatis.spring.annotation.MapperScan;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;

@Configuration
@MapperScan(basePackages = "com.dfbz.dao")//设置扫描mybatis接口包  ,必须使用tkmapper提供的扫描注解
@ComponentScan(basePackages = "com.dfbz.service")  //设置开启spring组件扫描
@EnableTransactionManagement//2.开启事务注解支持
public class SpringMyBatis  {

    //申明为spring管理的bean   <bean>
    @Bean
    public DruidDataSource getDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        //加载属性  自动读取类路径下的db.properties   传入给dataSource
        Properties properties = new Properties();
        try {
            //SpringMybatis.class.getClassLoader() 获取类加载器
            //getResourceAsStream("db.properties")  从类路径下开始查找资源
            properties.load(SpringMyBatis.class.getClassLoader().getResourceAsStream("db.properties"));
            dataSource.setFilters("stat,wall,slf4j");//开启自带的过滤器  stat用于监控sql
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        dataSource.configFromPropety(properties);
        return dataSource;
    }

    @Bean
    public SqlSessionFactoryBean getFactoryBean(DruidDataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);//设置数据源
        //替代原mybatis总配置文件的设置
        //当前类中存在两个Configuration类型，只能import一个  另外一个就需要通过包名.类名来声明
        tk.mybatis.mapper.session.Configuration configuration = new tk.mybatis.mapper.session.Configuration();
//        sqlSessionFactoryBean.setConfiguration();
        //设置通过Log4j2框架来记录日志
        configuration.setLogImpl(Log4j2Impl.class);
        //设置支持数据库经典设计风格到驼峰命名自动转换   user_id   ->  userId
        //tk自带的crud可以自动适配驼峰命名转换，非自带的还是需要设置
        configuration.setMapUnderscoreToCamelCase(true);

        sqlSessionFactoryBean.setConfiguration(configuration);//设置使用tk的configuration
//        声明分页拦截器插件，并设置为sqlSessionFactoryBean第三方插件
        PageInterceptor pageInterceptor = new PageInterceptor();
        pageInterceptor.setProperties(new Properties());//设置默认方言(对应数据库的sql语法)
//        Interceptor[] interceptors = {new PageInterceptor()};
        sqlSessionFactoryBean.setPlugins(pageInterceptor);
        return sqlSessionFactoryBean;
    }

    //事务管理器配置
    @Bean
    public DataSourceTransactionManager getDataSourceTransactionManager(DruidDataSource druidDataSource){
        return new DataSourceTransactionManager(druidDataSource);
    }

    @Bean(name="druidStatInterceptor")//设置druid 的 aop切面类
    public DruidStatInterceptor getDruidStatInterceptor(){
        DruidStatInterceptor druidStatInterceptor = new DruidStatInterceptor();

        return druidStatInterceptor;
    }


}
